package com.sparrow.manage.menu;

import com.sparrow.config.EnvironmentVariables;
import com.sparrow.manage.common.controller.StandardController;
import com.sparrow.manage.common.entity.result.Result;
import com.sparrow.manage.common.service.BaseService;
import com.sparrow.manage.common.tree.TreeTable;
import com.sparrow.manage.common.tree.TreeTableElement;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(EnvironmentVariables.MANAGE_ACCESS_PREFIX + "/menu")
public class MenuController extends StandardController<Menu> {

    private static final Logger logger = LoggerFactory.getLogger(MenuController.class);

    @Autowired
    private MenuService menuService;

    @RequestMapping("index")
    public String index(){
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "menu/index";
    }

    @RequestMapping("addPage")
    public String addMenuPage(String pid, Model model){
        if(StringUtils.isNotBlank(pid)){
            Menu menu = menuService.queryById(pid);
            model.addAttribute("pMenu", menu);
        }
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "menu/add";
    }

    @RequestMapping("editPage")
    public String editMenuPage(String id, Model model){
        Menu menu = menuService.queryById(id);
        model.addAttribute("menu", menu);
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "menu/edit";
    }

    @RequestMapping("selectTree")
    public String selectTree(String menuId){
        return EnvironmentVariables.MANAGE_TEMPLATE_STORE_PATH + "menu/selectTree";
    }

    @ResponseBody
    @RequestMapping("add")
    public Result add(Menu menu){
        return this.addEntity(menu);
    }

    @ResponseBody
    @RequestMapping("delete")
    public Result delete(String id){
        return this.removeEntity(id);
    }

    @ResponseBody
    @RequestMapping("update")
    public Result update(Menu menu){
        return this.updateEntity(menu);
    }


    @RequestMapping("tree")
    @ResponseBody
    public List<Menu> menuTree(){
        List<Menu> menus = menuService.queryTree();
        return menus;
    }

    @RequestMapping("treeTable")
    @ResponseBody
    public TreeTable<Menu> menuTreeTable(){
        TreeTable<Menu> treeTable = new TreeTable<Menu>();
        List<Menu> menus = menuService.queryList(new Menu());
        treeTable.setData(generateTreetableElements(menus));
        return treeTable;
    }

    private List<TreeTableElement<Menu>> generateTreetableElements(List<Menu> menus){
        if(menus == null) return null;

        List<TreeTableElement<Menu>> elements = new ArrayList<TreeTableElement<Menu>>();
        for(Menu menu : menus){
            TreeTableElement<Menu> element = new TreeTableElement<Menu>();
            element.setEntity(menu);
            element.setId(menu.getId());
            element.setpId(StringUtils.isBlank(menu.getpId()) ? "-1" : menu.getpId());
            elements.add(element);
        }

        return elements;
    }

    @Override
    protected BaseService getService() {
        return menuService;
    }

    @Override
    protected Result argumentsValidate(Menu menu) {
        return null;
    }
}
